<?php

namespace mpend\modules\team\models;

use Yii;
use common\models\workbook\WorkbookMember;
use common\helpers\Url;

/**
 * 班组记工本管理模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class WorkBookManageSearch extends \mpend\models\searchs\WorkBookSearch {

    /**
     * @inheritdoc
     */
    public function buildQuery($alias = 't') {
        $query = static::find();
        if ($alias) {
            $query->alias($alias);
            $alias .= '.';
        }
        $query->active(static::STATUS_ACTIVE);
        $query->andWhere([
            $alias . 'type' => static::TYPE_TEAM,
        ]);
        $query->joinWith('member member', false);
        $userId = Yii::$app->user->id;
        $query->andWhere([
            'or',
            [$alias . 'user_id' => $userId],
            [
                'and',
                ['member.user_id' => $userId],
                [
                    'member.role_id' => [
                        WorkbookMember::ROLE_ADMIN,
                        WorkbookMember::ROLE_SUPER,
                    ]
                ],
                ['member.deleted_at' => 0],
                ['member.status' => WorkbookMember::STATUS_ACTIVE],
            ],
        ]);
        $query->orderBy([
            $alias . 'created_at' => SORT_DESC,
        ]);
        $query->debug();
        return $query;
    }

    public function getStats() {
        return [
            [
                'title' => '总工钱（元）',
                'description' => $this->totalSalary,
                'icon' => '',
            ],
            [
                'title' => '今日记工（人）',
                'description' => $this->todayMarks,
                'icon' => '',
            ],
        ];
    }

    /**
     * 记工簿跳转地址
     * @return bool
     */
    public function getMplink() {
        $token = $this->getExtraAttribute('token');
        if (!$token) {
            return null;
        }
        if ($token->build === 1) {
            return parent::getMplink();
        }
        $uid = Yii::$app->user->id;
        if ($this->hasManageControl($uid)) {
            return [
                'type' => 'push',
                'route' => Url::to(['/pages/book/manager/main', 'id' => $this->id]),
            ];
        } elseif ($this->hasSuperviseControl($uid)) {
            return [
                'type' => 'push',
                'route' => Url::to(['/pages/book/team/main', 'id' => $this->id]),
            ];
        }
        return [
            'type' => 'push',
            'route' => Url::to(['/pages/book/intrant/main', 'id' => $this->id]),
        ];
    }

    public function fields() {
        return [
            'id',
            'title',
            'thumb',
            'description',
            'headman' => function() {
                return $this->headman ? $this->headman : '未设置';
            },
            'type',
            'createdTime',
            'teamwork' => function() {
                return boolval($this->teamwork);
            },
            'memberCount',
            'stats',
            'totalSalary',
            'uncleared',
            'recommendMembers',
            'actions',
            'mplink',
        ];
    }

}
